package org.jgs1904.practice.array;




import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author: 默苍璃
 * @date: 2022-09-09 15:25
 * <p>
 * 何实现对数组进行降序排序：
 */
public class Sort {
    public static void main(String[] args) {
        int[] ns = {28, 12, 89, 73, 65, 18, 96, 50, 8, 36};
        // 排序前:
        System.out.println(Arrays.toString(ns));
        // TODO:
        // 排序后:
        for (int i = 0; i < ns.length; i++) {
            //最新遍历次数 节约资源
            for (int j = 0; j < ns.length - i - 1; j++) {
                int temp = 0;
                if (ns[j] < ns[j + 1]) {
                    temp = ns[j];
                    ns[j] = ns[j + 1];
                    ns[j + 1] = temp;
                }
            }
        }

        System.out.println(Arrays.toString(ns));
        if (Arrays.toString(ns).equals("[96, 89, 73, 65, 50, 36, 28, 18, 12, 8]")) {
            System.out.println("测试成功");
        } else {
            System.out.println("测试失败");
        }
    }


    /**
     * 网友代码
     */
    @Test
    public void test01() {
        int[] ns = {28, 12, 89, 73, 65, 18, 96, 50, 8, 36};
        // 排序前:
        System.out.println(Arrays.toString(ns));
        // TODO:
        // 排序后:
        for (int a = ns.length - 1; a >= 0; a--) {
            ns[a] *= -1;
        }
        Arrays.sort(ns);
        for (int a = ns.length - 1; a >= 0; a--) {
            ns[a] *= -1;
        }

        System.out.println(Arrays.toString(ns));
        if (Arrays.toString(ns).equals("[96, 89, 73, 65, 50, 36, 28, 18, 12, 8]")) {
            System.out.println("测试成功");
        } else {
            System.out.println("测试失败");
        }

    }


}
